﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CSM.Models;
using CSM.Controllers;

namespace CSM.Views.Management
{
    public partial class UserRightSetupSelectLevel : CSMPageBase
    {
        CustomDataContext db = new CustomDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                TextBox_UserId.Text = HttpUtility.UrlDecode(Request.QueryString["userid"]).ToString();
                var myUserQuery = (from a in db.TCsm_JS_User where a.UserID == TextBox_UserId.Text select a).Single();
                TextBox_UserName.Text = myUserQuery.UserName;
                if (myUserQuery.KefuRightID == "" || myUserQuery.KefuRightID == null)
                {
                    var QueryToGridView = from b in db.TCrm_Sys_Items
                                          where b.ClassID=="khdj"
                                          select new
                                          {
                                              b.ItemID,
                                              b.ItemName,
                                              check = false
                                          };
                    GridView_LevelList.DataSource = QueryToGridView;
                    GridView_LevelList.DataBind();
                }
                else
                {
                    string[] aa = myUserQuery.KefuRightID.Split(',');

                    var QueryToGridView = from b in db.TCrm_Sys_Items
                                          where b.ClassID == "khdj"
                                          select new
                                          {
                                              b.ItemID,
                                              b.ItemName,
                                              check = aa.Contains(b.ItemID.ToString())
                                          };
                    GridView_LevelList.DataSource = QueryToGridView;
                    GridView_LevelList.DataBind();

                }
            }

        }
        protected void Button_Cancel_Command(object sender, CommandEventArgs e)
        {
            Server.Transfer("UserRightSetup.aspx");
        }

        protected void Button_Save_Command(object sender, CommandEventArgs e)
        {
            string myLevel = "";

            for (int i = 0; i < GridView_LevelList.Rows.Count; i++)
            {
                GridViewRow myrows = GridView_LevelList.Rows[i];
                if (((CheckBox)myrows.Cells[2].FindControl("Checkbox_Shop")).Checked)
                {
                    myLevel = myLevel + "," + myrows.Cells[0].Text;
                }
            }

            var myRightRow = (from a in db.TCsm_JS_User where a.UserID == TextBox_UserId.Text select a).Single();
            if (myLevel != "")
            {
                string xx = myLevel.Substring(1, myLevel.Length - 1);
                myRightRow.KefuRightID = xx;
            }
            else
            {
                myRightRow.KefuRightID = "";
            }
            try
            {
                db.SubmitChanges();
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "aa", " <script>tb_remove(); </script>");
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新等级权限成功'); </script>");
                Server.Transfer("UserRightSetup.aspx");
            }
            catch
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('更新等级权限失败'); </script>");
            }
        }
    }
}
